package com.itheima.health.service.impl;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.result.Result;
import com.itheima.health.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    /**
     * 体检预约
     * @param map
     * @return
     */
    @Override
    @Transactional
    public Result order(Map map) {
        //查看用户预约日期是否在可预约日期中
        String orderDate = (String) map.get("orderDate");
        OrderSetting orderSetting = null;
        try {               //string类型转换成date类型
             orderSetting = orderSettingDao.findByOrderDate(
                     new SimpleDateFormat("yyyy-MM-dd").parse(orderDate));
            if (orderSetting == null) {
                return new Result(false, MessageConst.SELECTED_DATE_CANNOT_ORDER);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //判断可预约人数和以预约人数
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (reservations >= number) {
            return new Result(false, MessageConst.ORDER_FULL);
        }

        //判断用户是否在重复预约
        Member member = memberDao.findByTelephone(map.get("telephone").toString());
        if (member != null) {
            Integer memberId = member.getId();
            Date order_date = null;
            try {
                order_date = new SimpleDateFormat("yyyy-MM-dd")
                        .parse(map.get("orderDate").toString());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            String setmealId = (String) map.get("setmealId");
            Order order = new Order(memberId, order_date,Order.ORDERTYPE_WEIXIN,Order.ORDERSTATUS_NO, Integer.parseInt(setmealId));
            List<Order> orderList = orderDao.findByCondition(order);
            if (!CollectionUtils.isEmpty(orderList)) {
                return new Result(false,MessageConst.HAS_ORDERED);
            }
        } else {
            memberDao.addMemberDao(Member.builder()
                    .name(map.get("name").toString())
                    .phoneNumber(map.get("telephone").toString())
                    .idCard(map.get("idCard").toString())
                    .sex(map.get("sex").toString())
                    .regTime(new Date())
                    .build());
        }

        //预约成功
        Order order = null;
        try {
            order = Order.builder()
                    .memberId(member.getId())
                    .orderDate(new SimpleDateFormat("yyyy-MM-dd").parse(orderDate))
                    .orderType(map.get("orderType").toString())
                    .setmealId(Integer.parseInt(map.get("setmealId").toString()))
                    .orderStatus(Order.ORDERSTATUS_NO)
                    .build();
            orderDao.addOrder(order);

            //设置已预约人数
            orderSetting.setReservations(orderSetting.getReservations() + 1);
            orderSettingDao.editReservationsByOrderDate(orderSetting);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Result(true,MessageConst.ORDERSETTING_SUCCESS,order);
    }

    /**
     * 查询订单
     * @param id
     * @return
     */
    @Override
    public Map findById(Integer id) {
        Map map = orderDao.findById(id);
        try {
            if (map != null) {
                map.put("orderDate",
                        new SimpleDateFormat("yyyy-MM-dd")
                                .parse(map.get("orderDate").toString()));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return map;
    }
}
